﻿<template>
  <div class="baodanEntity-container">
    <el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
      <el-form :model="queryParams" ref="queryForm" labelWidth="90">
        <el-row>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
            <el-form-item label="关键字">
              <el-input v-model="queryParams.searchKey" clearable="" placeholder="请输入模糊查询关键字" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="生效日期">
              <el-input v-model="queryParams.effectiveDate" clearable="" placeholder="请输入生效日期" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="保险公司名称">
              <el-input v-model="queryParams.companyName" clearable="" placeholder="请输入保险公司名称" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="产品名称">
              <el-input v-model="queryParams.productName" clearable="" placeholder="请输入产品名称" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="保单号">
              <el-input v-model="queryParams.productNumber" clearable="" placeholder="请输入保单号" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="投保人">
              <el-input v-model="queryParams.policyholder" clearable="" placeholder="请输入投保人" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="手机号">
              <el-input v-model="queryParams.phoneNumber" clearable="" placeholder="请输入手机号" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="保费">
              <el-input v-model="queryParams.cost" clearable="" placeholder="请输入保费" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="缴费期">
              <el-input v-model="queryParams.cycle" clearable="" placeholder="请输入缴费期" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="联系人">
              <el-input v-model="queryParams.contact" clearable="" placeholder="请输入联系人" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="保单状态">
              <el-input v-model="queryParams.status" clearable="" placeholder="请输入保单状态" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="结算备注">
              <el-input v-model="queryParams.remark" clearable="" placeholder="请输入结算备注" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="净费率">
              <el-input v-model="queryParams.rate" clearable="" placeholder="请输入净费率" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="团队结算净费">
              <el-input v-model="queryParams.teamCost" clearable="" placeholder="请输入团队结算净费" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="经纪公司">
              <el-input v-model="queryParams.jJCompany" clearable="" placeholder="请输入经纪公司" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="现结出资">
              <el-input v-model="queryParams.xjcz" clearable="" placeholder="请输入现结出资" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="公司收入情况">
              <el-input v-model="queryParams.gssrqk" clearable="" placeholder="请输入公司收入情况" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="收入比率">
              <el-input v-model="queryParams.srbl" clearable="" placeholder="请输入收入比率" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="收入金额">
              <el-input v-model="queryParams.srje" clearable="" placeholder="请输入收入金额" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="公司支出情况">
              <el-input v-model="queryParams.gszcqk" clearable="" placeholder="请输入公司支出情况" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="支出比率">
              <el-input v-model="queryParams.zcbl" clearable="" placeholder="请输入支出比率" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="支出金额">
              <el-input v-model="queryParams.zcje" clearable="" placeholder="请输入支出金额" />

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
            <el-form-item>
              <el-button-group style="display: flex; align-items: center;">
                <el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'baodanEntity:page'"> 查询
                </el-button>
                <el-button icon="ele-Refresh" @click="() => queryParams = {}"> 重置 </el-button>
                <el-button icon="ele-ZoomIn" @click="changeAdvanceQueryUI" v-if="!showAdvanceQueryUI"
                  style="margin-left:5px;"> 高级查询 </el-button>
                <el-button icon="ele-ZoomOut" @click="changeAdvanceQueryUI" v-if="showAdvanceQueryUI"
                  style="margin-left:5px;"> 隐藏 </el-button>
                <el-button type="primary" style="margin-left:5px;" icon="ele-Plus" @click="openAddBaodanEntity"
                  v-auth="'baodanEntity:add'"> 新增 </el-button>
                  <el-button icon="ele-FolderOpened" type="danger" style="margin-left:5px;" @click="batchDelBaodanEntity"
                  v-auth="'baodanEntity:batchDelete'"> 批量删除 </el-button>
                <el-button icon="ele-FolderOpened" type="success" style="margin-left:5px;" @click="exportBaodan"
                  v-auth="'baodanEntity:export'"> 导出 </el-button>
                <el-upload ref="uploadRef" :action="uploadUrl" accept=".xlsx" :headers="headersObj"
                  :show-file-list="false" :limit="1" :on-error="handleError" :on-exceed="handleExceed"
                  :on-success="handleSuccess">
                  <el-button type="warning" style="height: 24px;margin: 3px 5px 0 5px" icon="ele-Upload" v-auth="'baodanEntity:import'"> 导入 </el-button>
                </el-upload>
                <el-button icon="ele-Document" type="primary" link style="margin-left:5px;"
                  @click="downloadBaodanTemplate" v-auth="'baodanEntity:downloadTemplate'">下载模板 </el-button>
              </el-button-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="full-table" shadow="hover" style="margin-top: 5px">
      <el-table :data="tableData" stripe style="width: 100%" v-loading="loading" tooltip-effect="light" row-key="id"
        @sort-change="sortChange" @selection-change="handleSelectionChange" border="">
        <el-table-column type="selection" :selectable="selectable" width="40" align="center" />
        <el-table-column type="index" label="序号" width="55" align="center" />
        <el-table-column prop="effectiveDate" label="生效日期" show-overflow-tooltip="" sortable />
        <el-table-column prop="companyName" label="保险公司名称" show-overflow-tooltip="" />
        <el-table-column prop="productName" label="产品名称" show-overflow-tooltip="" />
        <el-table-column prop="productNumber" label="保单号" show-overflow-tooltip="" />
        <el-table-column prop="policyholder" label="投保人" show-overflow-tooltip="" />
        <el-table-column prop="phoneNumber" label="手机号" show-overflow-tooltip="" />
        <el-table-column prop="cost" label="保费" show-overflow-tooltip="" />
        <el-table-column prop="cycle" label="缴费期" show-overflow-tooltip="" />
        <el-table-column prop="contact" label="联系人" show-overflow-tooltip="" />
        <el-table-column prop="status" label="保单状态" show-overflow-tooltip="" />
        <el-table-column prop="remark" label="结算备注" show-overflow-tooltip="" />
        <el-table-column prop="rate" label="净费率" show-overflow-tooltip="" />
        <el-table-column prop="teamCost" label="团队结算净费" show-overflow-tooltip="" />
        <el-table-column prop="jJCompany" label="经纪公司" show-overflow-tooltip="" />
        <el-table-column prop="xjcz" label="现结出资" show-overflow-tooltip="" />
        <el-table-column prop="gssrqk" label="公司收入情况" show-overflow-tooltip="" />
        <el-table-column prop="srbl" label="收入比率" show-overflow-tooltip="" />
        <el-table-column prop="srje" label="收入金额" show-overflow-tooltip="" />
        <el-table-column prop="gszcqk" label="公司支出情况" show-overflow-tooltip="" />
        <el-table-column prop="zcbl" label="支出比率" show-overflow-tooltip="" />
        <el-table-column prop="zcje" label="支出金额" show-overflow-tooltip="" />
        <el-table-column label="修改记录" width="100" align="center" show-overflow-tooltip>
          <template #default="scope">
            <ModifyRecord :data="scope.row" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="210" align="center" fixed="right" show-overflow-tooltip=""
          v-if="auth('baodanEntity:detail') || auth('baodanEntity:update') || auth('baodanEntity:delete')">
          <template #default="scope">
            <el-button icon="ele-View" size="small" text="" type="success" @click="openViewBaodanEntity(scope.row)"
              v-auth="'baodanEntity:detail'"> 查看 </el-button>
            <el-button icon="ele-Edit" size="small" text="" type="primary" @click="openEditBaodanEntity(scope.row)"
              v-auth="'baodanEntity:update'"> 编辑 </el-button>
            <el-button icon="ele-Delete" size="small" text="" type="danger" @click="delBaodanEntity(scope.row)"
              v-auth="'baodanEntity:delete'"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination v-model:currentPage="tableParams.page" v-model:page-size="tableParams.pageSize"
        :total="tableParams.total" :page-sizes="[10, 20, 50, 100, 200, 500]" size="small" background=""
        @size-change="handleSizeChange" @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper" />
      <printDialog ref="printDialogRef" :title="printBaodanEntityTitle" @reloadTable="handleQuery" />
      <editDialog ref="editDialogRef" :title="editBaodanEntityTitle" :isdisabled="editBaodanEntityIsdisabled"
        @reloadTable="handleQuery" />
    </el-card>
  </div>
</template>

<script lang="ts" setup="" name="baodanEntity">
import { ref } from "vue";
import { genFileId, ElMessageBox, ElMessage } from "element-plus";
import type {UploadInstance, UploadProps, UploadUserFile,UploadRawFile } from 'element-plus'
import { auth } from '/@/utils/authFunction';
import ModifyRecord from '/@/components/table/modifyRecord.vue';
import { downloadByData, getFileName } from '/@/utils/download';
import { Local } from '/@/utils/storage';

import printDialog from '/@/views/system/print/component/hiprint/preview.vue'
import editDialog from '/@/views/bxm/baodanEntity/component/editDialog.vue'
import { pageBaodanEntity, deleteBaodanEntity, batchDeleteBaodanEntity, exportBaodanEntity, downloadBaodanEntityTemplate } from '/@/api/bxm/baodanEntity';
import { forEach } from "lodash-es";

const showAdvanceQueryUI = ref(false);
const printDialogRef = ref();
const editDialogRef = ref();
const loading = ref(false);
const tableData = ref<any>([]);
const queryParams = ref<any>({});
const tableParams = ref({
  page: 1,
  pageSize: 10,
  field: 'createTime', // 默认的排序字段
  order: 'descending', // 排序方向
  descstr: 'descending', // 降序排序的关键字符
  total: 0 as any,
});


interface Bx {
  id: number
}
const multipleSelection = ref<Bx[]>([])
const selectable = (row: Bx) => ![1, 2].includes(row.id)

const headersObj = {
	Authorization: 'Bearer ' + Local.get('access-token'),
};
const baseUrl = import.meta.env.VITE_API_URL;
const uploadRef = ref<UploadInstance>();
const uploadUrl = baseUrl + '/api/baodanEntity/import';
const downloadLoading = ref(false);
const printBaodanEntityTitle = ref("");
const editBaodanEntityTitle = ref("");
const editBaodanEntityIsdisabled = ref(true);

const handleSelectionChange = (val: Bx[]) => {
  multipleSelection.value = val
}

// 改变高级查询的控件显示状态
const changeAdvanceQueryUI = () => {
  showAdvanceQueryUI.value = !showAdvanceQueryUI.value;
}

// 查询操作
const handleQuery = async () => {
  loading.value = true;
  var res = await pageBaodanEntity(Object.assign(queryParams.value, tableParams.value));
  tableData.value = res.data.result?.items ?? [];
  tableParams.value.total = res.data.result?.total;
  loading.value = false;
};

// 列排序
const sortChange = async (column: any) => {
  tableParams.value.field = column.prop;
  tableParams.value.order = column.order;
  await handleQuery();
};

// 打开新增页面
const openAddBaodanEntity = () => {
  editBaodanEntityTitle.value = '添加保单';
  editBaodanEntityIsdisabled.value = false;
  editDialogRef.value.openDialog({});
};

// 打开打印页面
const openPrintBaodanEntity = async (row: any) => {
  printBaodanEntityTitle.value = '打印保单查询';
}

// 打开查看页面
const openViewBaodanEntity = (row: any) => {
  editBaodanEntityTitle.value = '查看保单详情';
  editBaodanEntityIsdisabled.value = true;
  editDialogRef.value.openDialog(row);
};

// 打开编辑页面
const openEditBaodanEntity = (row: any) => {
  editBaodanEntityTitle.value = '编辑保单查询';
  editBaodanEntityIsdisabled.value = false;
  editDialogRef.value.openDialog(row);
};

// 删除
const delBaodanEntity = (row: any) => {
  ElMessageBox.confirm(`确定要删除吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      await deleteBaodanEntity(row);
      handleQuery();
      ElMessage.success("删除成功");
    })
    .catch(() => { });
};

// 批量删除
const batchDelBaodanEntity = () => {
  ElMessageBox.confirm(`确定要删除吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      await batchDeleteBaodanEntity(multipleSelection.value);
      handleQuery();
      ElMessage.success("删除成功");
    })
    .catch(() => { });
};

// 改变页面容量
const handleSizeChange = (val: number) => {
  tableParams.value.pageSize = val;
  handleQuery();
};

// 改变页码序号
const handleCurrentChange = (val: number) => {
  tableParams.value.page = val;
  handleQuery();
};

// 导出
const exportBaodan = async () => {
  downloadLoading.value = true;
  var res = await exportBaodanEntity(Object.assign(queryParams.value, tableParams.value));
  downloadLoading.value = false;

  var fileName = getFileName(res.headers);
  downloadByData(res.data as any, fileName);
};

//上传文件
const handleExceed: UploadProps['onExceed'] = (files) => {
  loading.value = true;
	uploadRef.value!.clearFiles();
	const file = files[0] as UploadRawFile;
  file.uid = genFileId();
  uploadRef.value!.handleStart(file);
  uploadRef.value!.submit();
};

//上传成功时回调
const handleSuccess = (response: any) => {
  loading.value = false;
	ElMessage.success("成功导入：" + response.result + "条数据。");
	handleQuery();
};

//上传失败时回调
const handleError = (error: Error) => {
  loading.value = false;
	ElMessage.error(error);
};

// 下载模板
const downloadBaodanTemplate = async () => {
  downloadLoading.value = true;
  var res = await downloadBaodanEntityTemplate();
  downloadLoading.value = false;
  var fileName = getFileName(res.headers);
  downloadByData(res.data as any, fileName);
};

handleQuery();

</script>
<style scoped>
:deep(.el-input),
:deep(.el-select),
:deep(.el-input-number) {
  width: 100%;
}
</style>
